<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>C++ Tutorial: make</title>
  <meta
 content="C++ Tutorial: make"
 name="description" />
  <meta
 content="C++ Tutorial, make"
 name="keywords" />
  <meta http-equiv="Content-Type"
 content="text/html; charset=ISO-8859-1" />
  <link type="text/css" rel="stylesheet" href="../images/bogostyleWidePre.css" />
</head>
<body>
<div id="page" align="center">
<div id="content" style="width: 800px;">
<div id="logo">
<div class="whitetitle" style="margin-top: 70px;">bogotobogo </div>
</div>
<div id="topheader">
<div class="headerbodytext" align="left"><br />
<strong>Bogotobogo</strong><br />
contact@bogotobogo.com </div>
<div class="smallgraytext" id="toplinks"><a href="../index.html">Home</a>
| <a href="../sitemap.html">Sitemap</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="menu">
<div class="smallwhitetext" style="padding: 9px;" align="right"><a
 href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">Products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="submenu">
<div class="smallgraytext" style="padding: 9px;" align="right">
<a href="../gif.html">Gif</a> 
|<a href="../java_applet.html">JavaApplet/Web Start</a>
| <a href="../flash.html">Flash</a>
| <a href="../shockwave.html">ShockWave</a>
| <a href="../svg.html">SVG</a>
| <a href="../iPhone.html">iPhone/iPad</a>
| <a href="../android.html">Android</a>
| <a href="../OnHTML5.html">HTML5</a>
| <a href="../Algorithms/algorithms.html">Algorithms</a>
| <a href="../News/NewsMain.html">News</a>
| <a href="../cplusplus/cpptut.html">C++</a>
| <a href="../Java/tutorial/on_java.html">Java</a>
| <a href="../php/phptut.html">PHP</a>
| <a href="../DesignPatterns/introduction.html">Design Patterns</a>
| <a href="../CSharp/.netframework.html">C#</a>
| <a href="../python/pytut.html">Python</a> 
| <a href="../VisualBasicSQL/introduction.html">Visual Basic</a>
</div>
</div>

<br />
<div id="contenttext">

<!-- Use of this code assumes agreement with the Google Custom Search Terms of Service. -->
<!-- The terms of service are available at http://www.google.com/cse/docs/tos.html -->
<form name="cse" id="searchbox_demo" action="http://www.google.com/cse">
  <input type="hidden" name="cref" value="" />
  <input type="hidden" name="ie" value="utf-8" />
  <input type="hidden" name="hl" value="" />
  <input name="q" type="text" size="40" />
  <input type="submit" name="sa" value="Search" />
</form>
<script type="text/javascript" src="http://www.google.com/cse/tools/onthefly?form=searchbox_demo&lang="></script>

<br />
<div style="padding: 10px;"><span class="titletext">C++ Tutorial - make</span></div>

<div id="bookmarkshare">
<script type="text/javascript">var addthis_config = {"data_track_clickback":true};</script>
<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=khhong7"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=khhong7"></script>
</div>

<img src="../images/cplusplus/cpp_logo.jpg" alt="cplusplus logo"/>

<div class="bodytext" style="padding: 12px;" align="justify"> 
<div class="subtitle_2nd" id="FullList">Full List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors (Function Objects)</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL)</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="stl3_iterators.html">Standard Template Library (STL) III - Iterators</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="stackunwinding.html">Stack Unwinding</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="make.html">make</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>
   </li>

</ul>
</div>
<br />

<br />
<br />
<div class="bodytext" style="padding: 12px;" align="justify">
<div class="subtitle" id="make">make</div>
<br />
<div class="subtitle_2nd" id="cmdlineBuild">Command Line Build on Windows</div>
<p>Visual C++ provides command-line tools for programmers who prefer to build their applications from the command prompt. 
If you want to use the command line to build a project created in Visual C++, you can use one of the following: </p>
<ul>
	<li> <strong>CL</strong>
	<p>Use the compiler (<strong>cl.exe</strong>) to compile and link source code files. </p>
	</li>
	<li><strong>Link</strong>
	<p>Use the linker (<strong>link.exe</strong>) to link compiled object files. </p>
	</li>
	<li><strong>MSBuild (Visual C++)</strong>
	<p>Use MSBuild to build Visual C++ projects and Visual Studio solutions from the command line.
	 Invoking this utility is equivalent to running the Build project or 
	Build Solution command in the Visual Studio integrated development environment.</p>
	</li>
	<li><strong>DEVENV</strong>
	<p>Use DEVENV combined with a command line switch, such as /Build or /Clean, 
	to perform certain build commands without displaying the Visual Studio IDE. </p>
	</li>
	<li><strong>NMake</strong>
	<p>Use NMake to automate tasks that build Visual C++ projects.</p>
	</li>
</ul>

<div class="subtitle_2nd" id="nmake">nmake</div>
<p>This <strong>nmake</strong> section assumes that we have Visual C++ 2008.</p>
<p>When we run <strong>nmake</strong>,  it looks for  <strong>makefile</strong> in the current directory. We can specify a file using the <p>
<pre>
nmake -f myfile option.
</pre>
<p>
While it may look like <strong>cl.exe</strong> is doing all the work, <strong>cl.exe</strong> does the compiling 
and<strong> link.exe</strong> does the linking. 
For all the compile options we can use <strong>cl</strong> and for all the linker options we can use <strong>link</strong>
We can run <strong>nmake</strong> without a target or we can specify a target such as <strong>nmake clean</strong>. 
Without a target <strong>nmake</strong> will look for the first available target in the <strong>makefile</strong> and build it.
</p>
<p>Here is the example of <strong>nmake</strong> calling <strong>makefile</strong>.</p>
<img src="images/make/nmakeRun.png" alt="nmakeRun"/>
<p>Here are the files needed:</p>
<ol>
	<li>C:\Testing\makefile 
<pre>
myHOME = C:\Testing
INCS=$(myHOME)\include
LIBS=$(myHOME)\lib

mybuild: main.obj foo.obj
	cl /o myTest.exe main.obj foo.obj /link  \
		/LIBPATH:$(LIBS) myLib.lib /NODEFAULTLIB:libcmt.lib 
main.obj: main.cpp
	cl /c /EHsc main.cpp -I $(INCS)
foo.obj: foo.cpp
	cl /c /EHsc foo.cpp -I $(INCS)
 
all:mybuild

clean:
   del *.exe *.obj
</pre>
	</li>
	<li>C:\Testing\main.cpp
<pre>
#include &lt;iostream&gt;
#include "foo.h"
using namespace std;

int main(void)
{
    cout << "main: This is test!" << endl;
    foo();
    return 0;
}

</pre>
	</li>
	<li>C:\Testing\foo.cpp
<pre>
#include &lt;iostream&gt;
#include "d.h "
using namespace std;

void foo()
{
  	cout << "foo(): This is from foo!" << endl;
	HelloWorld();
}
</pre>
	</li>
	<li>C:\Testing\include\foo.h
<pre>
void foo();
</pre>
	</li>
	<li>C:\Testing\include\d.h
<pre>
// d.h
#pragma once  
void HelloWorld();
</pre>
	</li>
	<li>C:\Testing\lib\myLib.lib <br />
		This library has a function "HelloWorld(), and it just prints out "Hello world!"
		<ol>
			<li>d.h
<pre>
// d.h
#pragma once  
void HelloWorld();
</pre>
			</li>
			<li>i.cpp
<pre>
// i.cpp
#include <stdio.h> 
// For the printf() function
#include "d.h"

void HelloWorld() 
{    
	printf("Hello world!");
}
</pre>
			</li>
		</ol>
	</li>
</ol>
<p>Here is the <a href="files/Testing.zip" target="blakn">Testing.zip</a> file.</p>
<p>There is couple of things we should know about the <strong>makefile</strong>:</p>
<ul>
	<li>Defines variables that will be used in the compiling and the linking. Similar to environment variables.
<pre>
myHOME = C:\Testing
INCS=$(myHOME)\include
LIBS=$(myHOME)\lib
</pre>
	</li>
	<li>Defines a target called <strong>mybuild</strong> that depends on other <strong>targets</strong> "main.obj" and "foo.obj".
We want to make sure that the dependencies are built.
<pre>
mybuild: main.obj foo.obj
</pre>

	</li>
	<li>Here it finds the target <strong>main.obj</strong> required at dependency line. 
It depends on the cpp source file <strong>main.cpp</strong> so now we go to the next line to compile the file. 
This is a compile only <strong>/c</strong> option. 
As you can see we are using <strong>-I $(INCS)</strong> which is specifying the directory to search for include files.
<pre>
main.obj: main.cpp 
	cl /c /EHsc main.cpp -I $(INCS) 
foo.obj: foo.cpp 
	cl /c /EHsc foo.cpp -I $(INCS) 

</pre>
	</li>
	<li>
	It builds <strong>main.obj</strong> and <strong>foo.obj</strong> targets.
	</li>
	<li>Once all the dependencies are built, it comes back to line which defines dependency. Then it builds the target. 
Here we are using <strong>/link</strong> to give the linker options which in this case is the LIBPATH to the <strong>myLib.lib</strong>.
<pre>
cl /o myTest.exe main.obj foo.obj /link \
		 /LIBPATH:$(LIBS) myLib.lib /NODEFAULTLIB:libcmt.lib 
</pre>
	</li>

	<li> /NODEFAULTLIB  Ignores all (or specified) default libraries when resolving external references.</li>
	<li>LIBCMT.LIB is a reentrant library for creating multithread programs. </li>
	<li>The MSVCRT.LIB library, which calls code in the shared MSVCRT70.DLL, is also reentrant.</li>
	<li>/EHsc: catch clause will not catch asynchronous exceptions.</li>
</ul>




<br /><br /><br /><br /><br /><br />
<div class="subtitle_2nd" id="FullList">Full List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors (Function Objects)</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL)</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="stl3_iterators.html">Standard Template Library (STL) III - Iterators</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="make.html">make</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>
   </li>
</ul>
<br /><br />




<br />


<br />
<br />
<br />


</div>
</div>
<div class="smallgraytext" id="footer"><a href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="#">Contact Us</a>
| Bogotobogo &copy; 2011 | <a target="_blank" href="http://www.bogotobogo.com">Bogotobogo </a>
</div>
</div>
</div>
</body>
</html>
